package cn.lbd.zk;

import org.apache.zookeeper.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ZKGetChild {
    private CountDownLatch countDownLatch = null;
    private ZooKeeper zooKeeper = null;
    private String socketAddress = "39.97.191.1:2181";

    @Before
    public void init() throws Exception {
        countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(socketAddress, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("连接建立成功");
                    //连接成功，通知计数器-1
                    countDownLatch.countDown();
                    System.out.println("使用连接对象监视器");
                    System.out.println(event.getState());
                    System.out.println(event.getType());
                }
            }
        });
        countDownLatch.await();
        System.out.println("会话ID => " + zooKeeper.getSessionId());
    }

    @After
    public void destroy() throws InterruptedException {
        zooKeeper.close();
    }

    //同步
    @Test
    public void test1() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/create", false);
        for (String child : children) {
            System.out.println(child);
        }
    }

    //异步
    @Test
    public void test2() throws InterruptedException {
        zooKeeper.getChildren("/create", false, new AsyncCallback.ChildrenCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, List<String> children) {
                if (rc == 0) {
                    System.out.println("创建节点成功");
                }else {
                    System.out.println("创建节点失败");
                }
                System.out.println(path);
                System.out.println(ctx);
                for (String child : children) {
                    System.out.println(child);
                }
            }
        },"这是一个上下文");
    }

    @Test
    public void test3() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/create", true);
        for (String child : children) {
            System.out.println(child);
        }
        TimeUnit.SECONDS.sleep(50);
    }

    @Test
    public void test4() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/create", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("自定义");
                System.out.println(event.getState());
                System.out.println(event.getType());
            }
        });
        for (String child : children) {
            System.out.println(child);
        }
        TimeUnit.SECONDS.sleep(50);
    }

    @Test
    public void test5() throws KeeperException, InterruptedException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("自定义");
                System.out.println(event.getState());
                System.out.println(event.getType());
                try {
                    zooKeeper.getChildren("/create", this);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        List<String> children = zooKeeper.getChildren("/create", watcher);
        for (String child : children) {
            System.out.println(child);
        }
        TimeUnit.SECONDS.sleep(50);
    }

    @Test
    public void test6() throws KeeperException, InterruptedException {
        List<String> children1 = zooKeeper.getChildren("/create", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getState());
                System.out.println(event.getType());
            }
        });
        List<String> children2 = zooKeeper.getChildren("/create", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getState());
                System.out.println(event.getType());
            }
        });
        for (String child : children1) {
            System.out.println(child);
        }

        for (String child : children2) {
            System.out.println(child);
        }
        TimeUnit.SECONDS.sleep(50);
    }
}
